package com.myslef.teaching_platfrom.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.myslef.teaching_platfrom.domain.Message;
import com.myslef.teaching_platfrom.domain.users.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MessageMapper extends BaseMapper<Message> {
    @Select("select id, user_name, create_time from " +
            " (select u2.id, u2.user_name, m.create_time create_time from message m join user u on m.from_id = u.id join user u2 on m.to_id = u2.id " +
            " where m.from_id = #{id}" +
            " union " +
            " select u.id, u.user_name, m.create_time create_time from message m join user u on m.from_id = u.id join user u2 on m.to_id = u2.id where m.to_id = #{id}) " +
            " as subquery " +
            " group by id order by  create_time")
    List<User> getUserListById(Integer id);

    @Select("SELECT * FROM message WHERE (from_id = #{id1} and to_id = #{id2}) or (to_id = #{id1} and from_id = #{id2} )")
    List<Message> getMessageListById(Integer id1, Integer id2);
}
